(function(){
    "use strict";
    app.controller('createDeploy', ['$scope', 'httpLoad', '$rootScope','$modal','$state','$timeout','LANGUAGE',
        function($scope, httpLoad, $rootScope, $modal,$state, $timeout,LANGUAGE) {
            $rootScope.moduleTitle = '应用管理 > 应用部署';//定义当前页
            $rootScope.link = '/statics/css/common.css';//引入页面样式
            $scope.showGao = function($event){
                $($event.target).parent().parent().parent().next().show();
                $($event.target).hide();
                $($event.target).next().show();
            }
            $scope.hideGao = function($event){
                $($event.target).parent().parent().parent().next().hide();
                $($event.target).hide();
                $($event.target).prev().show();
            }
            $scope.env ={};
            $scope.disk ={};
            $scope.port ={};
            $scope.rowEnvList = [
            ];
            $scope.rowDiskList = [
            ];
            $scope.rowPortList = [
            ];
            $scope.imageName = "";
            $scope.imageTag = "";
            $scope.flag_deploy=1;
            $scope.firstStep=true;
            $scope.firstBule=true;
            $scope.firstGrey=false;
            $scope.secondBule=false;
            $scope.secondGrey=false;
            $scope.thirdBule=false;
            $scope.thirdGrey=false;

            $scope.imgFirst=true;
            $scope.imgSecond=false;
            $scope.imgThird=false;
            $scope.shopFirst=false;
            $scope.shopSecond=false;
            $scope.shopThird=false;
            $scope.bianFirst=false;
            $scope.bianSecond=false;

            $scope.choseFirst = function(){
                $scope.flag_deploy=1;
                $scope.firstBule=true;
                $scope.firstGrey=false;
                $scope.secondBule=false;
                $scope.secondGrey=false;
                $scope.thirdBule=false;
                $scope.thirdGrey=false;
                $scope.imgFirst=true;
                $scope.imgSecond=false;
                $scope.imgThird=false;
                $scope.shopFirst=false;
                $scope.shopSecond=false;
                $scope.shopThird=false;
                $scope.bianFirst=false;
                $scope.bianSecond=false;
            };
            $scope.choseSecond = function(){
                $scope.flag_deploy=2;
                $scope.firstBule=false;
                $scope.firstGrey=false;
                $scope.secondBule=true;
                $scope.secondGrey=false;
                $scope.thirdBule=false;
                $scope.thirdGrey=false;
                $scope.imgFirst=false;
                $scope.imgSecond=false;
                $scope.imgThird=false;
                $scope.shopFirst=true;
                $scope.shopSecond=false;
                $scope.shopThird=false;
                $scope.bianFirst=false;
                $scope.bianSecond=false;
            };
            $scope.choseThird = function(){
                $scope.flag_deploy=3;
                $scope.firstBule=false;
                $scope.firstGrey=false;
                $scope.secondBule=false;
                $scope.secondGrey=false;
                $scope.thirdBule=true;
                $scope.thirdGrey=false;
                $scope.imgFirst=false;
                $scope.imgSecond=false;
                $scope.imgThird=false;
                $scope.shopFirst=false;
                $scope.shopSecond=false;
                $scope.shopThird=false;
                $scope.bianFirst=true;
                $scope.bianSecond=false;
            };
            $scope.next = function(){
                if($scope.flag_deploy==1){
                    $scope.firstStep=false;
                    $scope.SecondStep_img=true;
                    $scope.SecondStep_shop=false;
                    $scope.SecondStep_choreography=false;
                    $scope.imgFirst=false;
                    $scope.imgSecond=true;
                    $scope.imgThird=false;
                    $scope.shopFirst=false;
                    $scope.shopSecond=false;
                    $scope.shopThird=false;
                    $scope.bianFirst=false;
                    $scope.bianSecond=false;
                    $scope.param = {
                        rows: 10
                    };
                    $scope.getData = function(page){
                        $scope.param.page = page || $scope.param.page;
                        var params = {
                            page: $scope.param.page,
                            rows: $scope.param.rows,
                        };
                        httpLoad.loadData({
                            url:'/runtime/image/list',
                            data: params,
                            success:function(data){
                                if(data.rows&&data.rows.length!=0){
                                    $scope.imgList = data.rows;
                                    $scope.imageName = $scope.imgList[0].imageName;
                                    $scope.imageTag = $scope.imgList[0].imageTag;
                                    $scope.totalCount = data.records;
                                    $scope.isImageData = false;
                                }else{
                                    $scope.isImageData = true;
                                }
                            }
                        });
                    };
                    $scope.getData(1);
                }else if($scope.flag_deploy==2){
                    $scope.firstStep=false;
                    $scope.SecondStep_shop=true;
                    $scope.SecondStep_choreography=false;
                    $scope.imgFirst=false;
                    $scope.imgSecond=false;
                    $scope.imgThird=false;
                    $scope.shopFirst=false;
                    $scope.shopSecond=true;
                    $scope.shopThird=false;
                    $scope.bianFirst=false;
                    $scope.bianSecond=false;
                }else{
                    $scope.firstStep=false;
                    $scope.SecondStep_choreography=true;
                    $scope.SecondStep_shop=false;
                    $scope.thirdStep_shop=false;
                    $scope.imgFirst=false;
                    $scope.imgSecond=false;
                    $scope.imgThird=false;
                    $scope.shopFirst=false;
                    $scope.shopSecond=false;
                    $scope.shopThird=false;
                    $scope.bianFirst=false;
                    $scope.bianSecond=true;
                }
            }

            $scope.previous_shop_second = function(){
                $scope.firstBule=false;
                $scope.firstGrey=false;
                $scope.secondBule=true;
                $scope.secondGrey=false;
                $scope.thirdBule=false;
                $scope.thirdGrey=false;
                $scope.imgFirst=false;
                $scope.imgSecond=false;
                $scope.imgThird=false;
                $scope.shopFirst=true;
                $scope.shopSecond=false;
                $scope.shopThird=false;
                $scope.bianFirst=false;
                $scope.bianSecond=false;
                $scope.firstStep=true;
                $scope.SecondStep_shop=false;
                $scope.thirdStep_shop=false;
                $scope.SecondStep_choreography=false;
            }
            $scope.next_shop_second = function(){
                $scope.firstBule=false;
                $scope.firstGrey=false;
                $scope.secondBule=false;
                $scope.secondGrey=false;
                $scope.thirdBule=false;
                $scope.thirdGrey=false;
                $scope.imgFirst=false;
                $scope.imgSecond=false;
                $scope.imgThird=false;
                $scope.shopFirst=false;
                $scope.shopSecond=false;
                $scope.shopThird=true;
                $scope.bianFirst=false;
                $scope.bianSecond=false;
                $scope.firstStep=false;
                $scope.SecondStep_shop=false;
                $scope.thirdStep_shop=true;
                $scope.SecondStep_choreography=false;
            }
            $scope.previous_img_second = function(){
                $scope.firstBule=true;
                $scope.firstGrey=false;
                $scope.secondBule=false;
                $scope.secondGrey=false;
                $scope.thirdBule=false;
                $scope.thirdGrey=false;
                $scope.imgFirst=true;
                $scope.imgSecond=false;
                $scope.imgThird=false;
                $scope.shopFirst=false;
                $scope.shopSecond=false;
                $scope.shopThird=false;
                $scope.bianFirst=false;
                $scope.bianSecond=false;
                $scope.firstStep=true;
                $scope.SecondStep_img=false;
                $scope.ThirdStep_img=false;
                $scope.SecondStep_shop=false;
                $scope.thirdStep_shop=false;
                $scope.SecondStep_choreography=false;
            }
            $scope.next_img_second = function(){
                $scope.firstBule=false;
                $scope.firstGrey=false;
                $scope.secondBule=false;
                $scope.secondGrey=false;
                $scope.thirdBule=false;
                $scope.thirdGrey=false;
                $scope.imgFirst=false;
                $scope.imgSecond=false;
                $scope.imgThird=true;
                $scope.shopFirst=false;
                $scope.shopSecond=false;
                $scope.shopThird=false;
                $scope.bianFirst=false;
                $scope.bianSecond=false;
                $scope.firstStep=false;
                $scope.SecondStep_img=false;
                $scope.ThirdStep_img=true;
                $scope.SecondStep_shop=false;
                $scope.thirdStep_shop=false;
                $scope.SecondStep_choreography=false;
            }
            $scope.previous_shop_third = function(){
                $scope.firstBule=false;
                $scope.firstGrey=false;
                $scope.secondBule=false;
                $scope.secondGrey=false;
                $scope.thirdBule=false;
                $scope.thirdGrey=false;
                $scope.imgFirst=false;
                $scope.imgSecond=false;
                $scope.imgThird=false;
                $scope.shopFirst=false;
                $scope.shopSecond=true;
                $scope.shopThird=false;
                $scope.bianFirst=false;
                $scope.bianSecond=false;
                $scope.firstStep=false;
                $scope.SecondStep_shop=true;
                $scope.thirdStep_shop=false;
                $scope.SecondStep_choreography=false;
            }

            $scope.choose = function(item){
                $scope.imageName = item.imageName;
                $scope.imageTag = item.imageTag;
            }
            $scope.create_img = function(){
                httpLoad.loadData({
                    url:'/clusterEnv/getClusterByEnvId',
                    data: {},
                    success:function(data){
                        if(data.success){
                            $scope.ip = data.data.clusterServiceIp;
                            $scope.makeImg($scope.ip);
                        }else{
                            $scope.pop(data.message,"error");
                            return false;
                        }
                    }
                });
                $scope.makeImg = function(ip){
                    $scope.envList="";
                    for(var i = 0; i < $scope.rowEnvList.length; i++)
                    {
                        $scope.envList+=$scope.rowEnvList[i].key+":"+$scope.rowEnvList[i].value+";"
                    }
                    $scope.diskList="";
                    for(var i = 0; i < $scope.rowDiskList.length; i++)
                    {
                        $scope.diskList+="local:local-Name"+":"+$scope.rowDiskList[i].configVolumesPath+":"+"localFileSystem"+";"
                    }
                    $scope.portList="";
                    for(var i = 0; i < $scope.rowPortList.length; i++)
                    {
                        $scope.portList+=$scope.rowPortList[i].value+";"
                    }
                    httpLoad.loadData({
                        url:'/application/createAppFromImage.do',
                        data: {
                            "applicationCore.applicationCoreName":$scope.img.appName,
                            "applicationCore.applicationCoreUsername":"default",
                            "applicationCore.applicationCoreType":"kubernetes",
                            "instanceImage.configImageName":$scope.imageName,
                            "instanceImage.configImageTag":$scope.imageTag,
                            "configBasic.configBasicCpu":$scope.img.cpu,
                            "configBasic.configBasicMemory":$scope.img.mem,
                            // "configBasic.configBasicNetwork":"network1",
                            // "configBasic.configBasicImagePullSecret":"my-secret",
                            "configVolumesListString":$scope.diskList,
                            "configPortsListString":$scope.portList,
                            "configEnvsListString":$scope.envList,
                            "instanceReplica":$scope.img.instanceReplica,
                            "ip":ip,
                        },
                        success:function(data){
                            if(data.success){
                                $scope.pop(data.message);
                            }else{
                                $scope.pop(data.message,'error');
                            }
                        }
                    });
                }
            }
            $scope.previous_img_third = function(){
                $scope.firstBule=false;
                $scope.firstGrey=false;
                $scope.secondBule=false;
                $scope.secondGrey=false;
                $scope.thirdBule=false;
                $scope.thirdGrey=false;
                $scope.imgFirst=false;
                $scope.imgSecond=true;
                $scope.imgThird=false;
                $scope.shopFirst=false;
                $scope.shopSecond=false;
                $scope.shopThird=false;
                $scope.bianFirst=false;
                $scope.bianSecond=false;
                $scope.firstStep=false;
                $scope.SecondStep_img=true;
                $scope.ThirdStep_img=false;
                $scope.SecondStep_shop=false;
                $scope.thirdStep_shop=false;
                $scope.SecondStep_choreography=false;
            }
            $scope.create_shop = function(){

            }
            $scope.previous_choreography_second = function(){
                $scope.firstBule=false;
                $scope.firstGrey=false;
                $scope.secondBule=false;
                $scope.secondGrey=false;
                $scope.thirdBule=true;
                $scope.thirdGrey=false;
                $scope.imgFirst=false;
                $scope.imgSecond=false;
                $scope.imgThird=false;
                $scope.shopFirst=false;
                $scope.shopSecond=false;
                $scope.shopThird=false;
                $scope.bianFirst=true;
                $scope.bianSecond=false;
                $scope.firstStep=true;
                $scope.SecondStep_shop=false;
                $scope.thirdStep_shop=false;
                $scope.SecondStep_choreography=false;
            }
            $scope.create_choreography = function(){

            }
            $scope.chose_shop_img = function (row) {
                if(row.selected) return;
                $scope.shopServerList.forEach(function (item) {
                    item.selected = false;
                });
                row.selected = true;
            };
            $scope.cancel = function(){
                $state.go('app.application.develop');
            }
            function  ObjStoryEnv(key,value) //声明对象
            {
                this.key = key;
                this.value= value;
            }
            function  ObjStoryDisk(configVolumesPath) //声明对象
            {
                this.configVolumesPath= configVolumesPath;
            }
            function  ObjStoryPort(value) //声明对象
            {
                this.value= value;
            }
            $scope.addEnv = function(){
                var length = $scope.rowEnvList.length;
                for(var i = 0; i < length; i++){
                    if($scope.rowEnvList[i].key == $scope.env.key)
                    {
                        $scope.pop("Key已存在",'error');
                        return;
                    }
                }
                $scope.rowEnvList.push(new ObjStoryEnv($scope.env.key,$scope.env.value));
            }
            $scope.removeEnv = function(row){
                var length = $scope.rowEnvList.length;
                for(var i = 0; i < length; i++)
                {
                    if($scope.rowEnvList[i] == row)
                    {
                        $scope.rowEnvList.splice(i,1); //删除下标为i的元素
                        return;
                    }
                }
            }
            $scope.addDisk = function(){
                var length = $scope.rowDiskList.length;
                for(var i = 0; i < length; i++){
                    if($scope.rowDiskList[i].configVolumesPath == $scope.disk.path)
                    {
                        $scope.pop("path已存在",'error');
                        return;
                    }
                }
                $scope.rowDiskList.push(new ObjStoryDisk($scope.disk.path));
            }
            $scope.removeDisk = function(row){
                var length = $scope.rowDiskList.length;
                for(var i = 0; i < length; i++)
                {
                    if($scope.rowDiskList[i] == row)
                    {
                        $scope.rowDiskList.splice(i,1); //删除下标为i的元素
                        return;
                    }
                }
            }
            $scope.addPort = function(){
                var length = $scope.rowPortList.length;
                for(var i = 0; i < length; i++){
                    if($scope.rowPortList[i].value == $scope.port.value)
                    {
                        $scope.pop("端口已存在",'error');
                        return;
                    }
                }
                $scope.rowPortList.push(new ObjStoryPort($scope.port.value));
            }
            $scope.removePort = function(row){
                var length = $scope.rowPortList.length;
                for(var i = 0; i < length; i++)
                {
                    if($scope.rowPortList[i] == row)
                    {
                        $scope.rowPortList.splice(i,1); //删除下标为i的元素
                        return;
                    }
                }
            }
        }
    ]);
    //在线编辑指令
    app.directive('ngCodeMirror', [function () {
        return {
            restrict: 'EA',
            scope:{
            },
            link: function (scope, element, attrs) {
                var editor = $(element).find('.textarea')[0];
                angular.element(document.getElementById('upload_file')).on('change',function(){
                    //获取文件
                    var data = $(this)[0].files[0];
                    if (data) {
                        //将文件进行转码，转换为text
                        var reader = new FileReader();
                        reader.readAsText(data);
                        reader.onload = function (f) {
                            codeMirror.setValue(this.result);
                        }
                    }
                });
                //初始化在线代码编辑器
                var codeMirror = CodeMirror.fromTextArea(editor, {
                    theme: 'erlang-dark',
                    mode: 'shell',
                    // lineNumbers: true,
                    readOnly: false
                });
            }
        }
    }]);
    app.directive('ngCodeMirror2', [function () {
        return {
            restrict: 'EA',
            scope:{
                codeMirror: '='
            },
            link: function (scope, element, attrs) {
                var editor = $(element).find('.textarea')[0];
                //初始化在线代码编辑器
                var codeMirror = CodeMirror.fromTextArea(editor, {
                    theme: 'erlang-dark',
                    mode: 'shell',
                    lineNumbers: true,
                    readOnly: true
                });
                scope.codeMirror = codeMirror;
            }
        }
    }]);
})();